package com.hsd.controller;

import com.hsd.entity.Book;
import com.hsd.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/test")
public class RESTfulController {

    @RequestMapping(value = "{id}/{name}/{sex}", method = RequestMethod.POST)
    public void create(@PathVariable("id") Integer tid, @PathVariable String name, @PathVariable String sex) {
        System.out.println("id:" + tid);
        System.out.println("name:" + name);
        System.out.println("sex:" + sex);
        System.out.println("添加编号为" + tid + "的数据！");
    }

    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public void delete(@PathVariable("id") Integer tid) {
        System.out.println("tid:" + tid);
        System.out.println("删除编号为" + tid + "的数据！");
    }

    @RequestMapping(value = "{id}/{name}/{sex}", method = RequestMethod.PUT)
    public void update(@PathVariable Integer id, @PathVariable String name, @PathVariable String sex) {
        System.out.println("id:" + id);
        System.out.println("name:" + name);
        System.out.println("sex:" + sex);
        System.out.println("更新编号为" + id + "的数据！");
    }

    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public void read(@PathVariable("id") Integer tid) {
        System.out.println("tid:" + tid);
        System.out.println("查询编号为" + tid + "的数据！");
    }

    @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
    @ResponseBody
    public User respUserInfo(@PathVariable Integer id){
        System.out.println("前端传来的UserID:"+id);
        //创建user对象
        Book book = new Book("《JAVA基础》","李四");
        List<String> hobbies = new ArrayList<>();
        hobbies.add("羽毛球");
        hobbies.add("乒乓球");
        hobbies.add("爬山");
        User user = new User("张三","男",20,book,hobbies);
        return user;
    }

    @RequestMapping(value = "/user/list", method = RequestMethod.GET)
    @ResponseBody
    public List<User> respUserListInfo(){
        List<User> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            //创建user对象
            Book book = new Book("《JAVA基础》"+i,"李四"+i);
            List<String> hobbies = new ArrayList<>();
            hobbies.add("羽毛球"+i);
            hobbies.add("乒乓球"+i);
            hobbies.add("爬山"+i);
            User user = new User("张三"+i,"男"+i,20,book,hobbies);
            list.add(user);
        }
        return list;
    }

    @RequestMapping(value = "/user/json", method = RequestMethod.POST)
    public void respUserListInfo(@RequestBody User user){
        System.out.println(user);
    }
}
